package xj.graph2d.template.uml;

import xj.graph2d.DrawShape;
import xj.graph2d.DrawAttr;
import xj.graph2d.DrawShapeConstants;
import xj.graph2d.GraphEdgeBase;
import xj.graph2d.template.UMLClassDiagramTemplate;
import xj.graph2d.template.UMLPackageDiagramTemplate;
import xj.graph2d.template.UMLTemplate;
import xj.model.uml.UMLAssociation;

import static xj.graph2d.template.UMLClassDiagramTemplate.GENERALIZATION_COLOR;
import static xj.graph2d.template.UMLClassDiagramTemplate.MIXIN_COLOR;

public class UMLGeneralizationModelListener extends UMLAbstractModelListener {

  public UMLGeneralizationModelListener() {
  }

  public UMLGeneralizationModelListener(UMLTemplate template) {
    this(null, null, template);
  }

  public UMLGeneralizationModelListener(Object model, 
					DrawShape shape,
					UMLTemplate template) {
    super(model, shape, template);
  }

  @Override
  public void updateModel() {
    if (shape instanceof GraphEdgeBase && 
	model instanceof UMLAssociation) {
      GraphEdgeBase edge = (GraphEdgeBase) shape;
      UMLAssociation dmodel = (UMLAssociation) model;

    }
  }

  @Override
  public void updateShape() {
    if (shape instanceof GraphEdgeBase && 
	model instanceof UMLAssociation) {
      GraphEdgeBase edge = (GraphEdgeBase) shape;
      UMLAssociation dmodel = (UMLAssociation) model;
      switch (dmodel.getType()) { 
      case Generalization:
	setLineStyle(shape, DrawAttr.SOLID_LINE);
	if (!(template instanceof UMLPackageDiagramTemplate) && 
	    template instanceof UMLClassDiagramTemplate) { 
	  template.setPenColor(shape, GENERALIZATION_COLOR);
	}
	break;
      case Realization:
	setLineStyle(shape, DrawAttr.DASHED_LINE);
	if (!(template instanceof UMLPackageDiagramTemplate) && 
	    template instanceof UMLClassDiagramTemplate) { 
	  template.setPenColor(shape, GENERALIZATION_COLOR);
	}
	break;
      case MixIn:
	setLineStyle(shape, DrawAttr.SOLID_LINE);
	if (!(template instanceof UMLPackageDiagramTemplate) && 
	    template instanceof UMLClassDiagramTemplate) { 
	  template.setPenColor(shape, MIXIN_COLOR);
	}
	break;
      }
    }
  }

  public static DrawShape setLineStyle(DrawShape shape, int lineStyle) { 
    if (shape != null) {
      DrawAttr attr = shape.getDrawAttr();
      if (attr == null) { 
	attr = new DrawAttr();
	shape.setDrawAttr(attr);
      }
      attr.setLineStyle(lineStyle);
    }
    return shape; 
  }

}
